package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.web.http.HttpContentType;
import com.ts.api.http.org.entity.apply.OrgApplyShaoXinChangXin;
import com.ts.api.http.org.entity.dataexchange.OrgDataExchangeShaoXinChangXin;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

/**
 * 绍兴常鑫-信息流信贷-50
 * @ Author 江小游
 * @ Date 2025/5/15 10:48
 */
@ApiPlan(productId = "197", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5ShaoXinChangXin implements ApiOrgPhoneMd5ConvertHandler {
    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        apiDataExchangeParameter.setHttpContentType(HttpContentType.APPLICATION_FORM_URLENCODED);
        OrgDataExchangeShaoXinChangXin shaoXinChangXin = new OrgDataExchangeShaoXinChangXin();
        shaoXinChangXin.setMd5(apiDataExchangeParameter.getPhoneMd5().toLowerCase());
        return shaoXinChangXin;
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 12)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        apiApplyParameter.setHttpContentType(HttpContentType.APPLICATION_FORM_URLENCODED);
        OrgApplyShaoXinChangXin applyShaoXinChangXin=new OrgApplyShaoXinChangXin();
        ApiFieldConverter apiFieldConverter=ApiFieldConverter.create(apiApplyParameter);
        //（必填）客户姓名
        applyShaoXinChangXin.setName(apiApplyParameter.getUserName());
        //性别(“男”或“女”)
        applyShaoXinChangXin.setSex(apiFieldConverter.getSexText());
        //（必填）手机号
        applyShaoXinChangXin.setMobile(apiApplyParameter.getPhone());
        //贷款额度(如：20，即单位为万)
        applyShaoXinChangXin.setMoney(apiFieldConverter.getLoanAmountInt());
        //（必填）城市(不包含市，比如“杭州市”应该是“杭州”)
        applyShaoXinChangXin.setCity(apiApplyParameter.getCity().replace("市", ""));
        //年龄(如：39)
        applyShaoXinChangXin.setAge(apiFieldConverter.getAgeInt());
        //社保(“有”或“无”)
        applyShaoXinChangXin.setShebao(apiFieldConverter.hasSocialSecurityToStr());
        //公积金(“有”或“无”)
        applyShaoXinChangXin.setGongjijin(apiFieldConverter.hasProvidentFundToStr());
        //保单(“有”或“无”)
        applyShaoXinChangXin.setBaodan_is(apiFieldConverter.hasInsurancePolicyToStr());
        //营业执照（“有”或“无”）
        applyShaoXinChangXin.setLicense(apiFieldConverter.hasBizLicenseYearToStr());
        //房(“有”或“无”)
        applyShaoXinChangXin.setHouse(apiFieldConverter.hasRealEstateInfoToStr());
        //车(“有”或“无”)
        applyShaoXinChangXin.setCar(apiFieldConverter.hasCarInfoToStr());
        //代发工资(“是”或“否”)
        applyShaoXinChangXin.setIsbankpay(apiFieldConverter.hasSalaryBankChooseToStr());
        //（必填）贷款来源(固定值，默认为“后台申请”)
        applyShaoXinChangXin.setSource("后台申请");
        applyShaoXinChangXin.setMeiti("安心分期");
        //（选填）备注
        applyShaoXinChangXin.setRemark(apiApplyParameter.getFormRemark());
        return applyShaoXinChangXin;
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
